﻿using System;
using System.Data;

namespace Sails.DB.Helper
{
    /// <summary>
    /// 数据库转换工具类
    /// </summary>
    public class DBTools
    {
        public static string GetString(DataRow dr,string filedName)
        {
            if(dr.Table.Columns.Contains(filedName))
            {
                if(dr[filedName]!=null)
                {
                    if(dr[filedName]!= DBNull.Value)
                    {
                        return dr[filedName].ToString();
                    }
                }
            }
            return "";
        }

        public static long GetLong(DataRow dr,string filedName)
        {
            long res = 0;
            if(long.TryParse(GetString(dr,filedName),out res))
            {
                return res;
            }
            return 0;            
        }

        public static int GetInt(DataRow dr ,string filedName)
        {
            int res = 0;
            if (int.TryParse(GetString(dr, filedName), out res))
            {
                return res;
            }
            return 0;            
        }

        public static ushort GetUShortInt(DataRow dr, string filedName)
        {
            ushort res = 0;
            if (ushort.TryParse(GetString(dr, filedName), out res))
            {
                return res;
            }
            return 0;
        }

        public static float GetFloat(DataRow dr, string filedName)
        {
            float res = 0;
            if (float.TryParse(GetString(dr, filedName), out res))
            {
                return res;
            }
            return 0;
        }

        public static double GetDouble(DataRow dr, string filedName)
        {
            Double res = 0;
            if (Double.TryParse(GetString(dr, filedName), out res))
            {
                return res;
            }
            return 0;
        }


        public static decimal GetDecmal(DataRow dr, string filedName)
        {
            decimal res = 0;
            if (decimal.TryParse(GetString(dr, filedName), out res))
            {
                return res;
            }
            return 0;
        }



        public static bool GetBool(DataRow dr, string filedName)
        {
            int res = 0;
            string value = GetString(dr, filedName);
            if (int.TryParse(value, out res))
            {
                return res>0;
            }
            else
            {
                if(value.ToLower() == "true")
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }


        public static DateTime? GetDateTime(DataRow dr, string filedName)
        {
            DateTime res;
            if (DateTime.TryParse(GetString(dr, filedName), out res))
            {
                return res;
            }
            else
            {
                return DateTime.MinValue;
            }
        }

        public static string GetDateTime(DataRow dr, string filedName,string format)
        {
            DateTime res;
            if (DateTime.TryParse(GetString(dr, filedName), out res))
            {
                return res.ToString(format);
            }
            return "";
        }


        public static byte[] GetBinary(DataRow dr, string filedName)
        {
            if (dr.Table.Columns.Contains(filedName))
            {
                if (dr[filedName] != null)
                {
                    if (dr[filedName] != DBNull.Value)
                    {
                        return (byte[])dr[filedName];
                    }
                }
            }
            return null;
        }
    }
}
